package com.qingge.springboot.utils;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author linyanbin
 * @date 2023/8/15 10:56
 */
public class BeanUtilsPlus {
    /**
     * 利用BeanUtils.copyProperties方法实现批量复制List source到 List target中
     * @Param [source, target]
     * @return java.util.List<T>
     **/
    public static <T> List<T> batchCopyProperties(List<?> source, Class<T> target){
        List<T> result = new ArrayList<>();
        try {
            for (Object s : source) {
                T t = target.newInstance();
                BeanUtils.copyProperties(s, t);
                result.add(t);
            }
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        return result;
    }
}
